<?php
/**
 * 表单类模型
 *
 *@author hary <hary@taomee.com>
 */
class ads_zone_model extends model
{
	private $ad_zone_table;

	/**
	 * 构造函数
	 * @return max
	 */
	function __construct()
	{
		parent::__construct();

		$this->ad_zone_table = $this->sys_conf_var[ 'table' ][ 'ad_zone' ];
		$this->ad_type_table = $this->sys_conf_var[ 'table' ][ 'ad_media_type' ];
	}

	/**
	 * 通过key获取整个zone的数据
	 * @return max
	 */
	public function getAdsZoneByKey($key){
	
		$key = $this->formatData($key);

		$this->where("z_key",$key);

		$zone = $this->getDataInfo($this->ad_zone_table);
		
		return $zone;

	}

	/**
	 * 根据site查询zone
	 * @return array
	 */
	public function getAdsZonesBySite($id,$page=1,$page_size=6,$order=" sort desc"){

		if($page) $this->setPage($page);
			
		if($page_size) $this->setLimit($page_size);
			
		if($order) $this->setOrder('order by '.$order);

		$id = $this->formatData($id);

		$this->where("z_site",$id,'=','and',0);

		return $this->getDataList($this->ad_zone_table);
	}

	/**
	 * 激活ads
	 * @return max
	 */
	public function activeAdsZoneById($key,$flag){

		$key  = $this->formatData($key);
		$flag = intval($flag);

		$this->where('ad_key',$key);

		$this->addData('enabled',$flag,0);

		return $this->dataUpdate($this->ad_zone_table);
	}

	/**
	 * 添加一个广告区
	 * @param $ads
	 * @return boolean
	 */
	public function addAdsZone($zone){

		$zone = $this->formatData($zone);
			
		if(!is_array($zone)) return false;

		$zone['z_key'] 		  = md5(time());
		$zone['z_start_time'] = strtotime($zone['z_start_time']);
		$zone['z_end_time']   = strtotime($zone['z_end_time']);

		$this->addDataBatch2($zone,array('z_name','z_key','z_site','z_link_man','z_link_email','z_link_phone','z_width','z_height','z_style','sort'));

		$result = $this->dataInsert($this->ad_zone_table);

		return $result;
	}

	/**
	 * 根据key删除ads
	 * @return max
	 */
	public function delAdsZoneByKey($key){

		$key = $this->formatData($key);

		$this->where('z_key',$key);

		return $this->dataDel($this->ad_zone_table);
	}

	/**根据id删除ads
	 * @return boolean
	 */
	public function delAdsZoneById($id){

		if(!is_numeric($id)) return false;
			
		$this->where('z_id',$id,'=','and',0);

		return $this->dataDel($this->ad_zone_table);
	}

	/**
	 * 根据id获取获取ads
	 * @return max
	 */
	public function getAdsZoneById($id){

		if(!is_numeric($id)) return false;

		$this->where('z_id',$id,'=','and',0);

		return $this->getDataInfo($this->ad_zone_table);
	}

	/**
	 * 更改ads项
	 * @return boolean
	 */
	public function changeAdsZoneByKey($zone){

		$zone = $this->formatData($zone);
		$key  = $zone['z_key'];
			
		if(!is_array($zone)) return false;

		$zone['z_start_time'] = strtotime($zone['z_start_time']);
		$zone['z_end_time']   = strtotime($zone['z_end_time']);

		$this->addDataBatch2($zone,array('z_name','z_key','z_site','z_link_man','z_link_email','z_link_phone','z_width','z_height','z_style','sort'));

		$this->where('z_key',$key);

		return $this->dataUpdate($this->ad_zone_table);

	}

	/**
	 * 根据关键字获取多个ads
	 * @return max
	 */
	public function getAdsZonesByKeywords($keywords,$page='1',$page_size='6',$order='sort desc'){

		$keywords = $this->formatData($keywords);

		if(empty($keywords))
		{
			$sql = "select * from $this->ad_zone_table ";
		}
		else
		{
			$sql = "select * from $this->ad_zone_table where
   				z_name like '%$keywords%' or 
   				z_site like '%$keywords%' or 
   				z_link_man like '%$keywords%'  or 
   				z_link_phone like '%$keywords%' or 
   				z_link_email like '%$keywords%'";
		}
			
		if($page) $this->setPage($page);
			
		if($page_size) $this->setLimit($page_size);
			
		if($order) $this->setOrder('order by '.$order);
			
		return $this->customQuery($sql);

	}

	/**
	 * 根据时间进行ads的查询
	 * @return max
	 */
	public function getAdsZoneByTime($start,$end,$page='1',$page_size='6',$order='sort desc'){

		if(!is_numeric($start) || !is_numeric($end)) return false;
			
		$sql = "select * from $this->ad_zone_table where  z_start_time <= $start and z_end_time >= $end ";
			
		if($page) $this->setPage($page);

		if($page_size) $this->setLimit($page_size);

		if($order) $this->setOrder('order by '.$order);
		
		return $this->customQuery($sql);
	}

}